<?php
class SearchXMLDB
{
    /**
     * @return array[int][string]string
     * @param array[string]string $nameParts
     * @param array[string]string $options
     */
    function getPeople($nameParts, $options)
    {
        global $db;
        $data = /*.(array[int][string]string).*/ array();
        $sql = <<< EOT
            SELECT
                Person.ID, 
                Person.Surname, 
                Person.FirstNames, 
                Person.FormerName, 
                Person.NeeName, 
                Person.AlsoKnownAs, 
                Person.DeathDate, 
                Person.BirthDate, 
                Person.Age AS 'Age', 
                Person.AgeMeasure AS 'AgeMeasure', 
                CityTown.Name as 'CityTown', 
                c2.Name as 'FormerCityTown', 
                Region.Name AS 'Region', 
                Country.Name AS 'Country', 
                Charity.Name AS 'Charity', 
                FuneralHome.Name AS 'FuneralHome',
                Person.OtherInfo As 'OtherInfo'
            FROM Person 
                LEFT JOIN Region
                    ON Person.RegionID = Region.ID 
                LEFT JOIN Country
                    ON Country.ID = Person.CountryID 
                LEFT JOIN CityTown AS c2
                    ON Person.FormerCityTownID = c2.ID 
                LEFT JOIN Charity
                    ON Charity.ID = Person.CharityID 
                LEFT JOIN FuneralHome
                    ON FuneralHome.ID = Person.FuneralHomeID 
                LEFT JOIN CityTown
                    ON CityTown.ID = Person.CityTownID
EOT;
        if (!empty($options['where'])) {
            $sql .= ' WHERE ' . $options['where'];
        }
        if (!empty($options['orderBy'])) {
            $sql .= ' ORDER BY ' . $options['orderBy'];
        }
        if (!empty($options['limit'])) {
            $sql .= ' LIMIT ' . $options['limit'];
        }
        $sql = sprintf($sql,
            $db->escape($nameParts['firstName']),
            $db->escape($nameParts['akaName']),
            $db->escape($nameParts['neeName']),
            $db->escape($nameParts['lastName'])
        );
        $result = $db->query($sql);
        while (TRUE) {
            $row = $db->fetch($result);
            if ($row === FALSE) {
                break;
            }
            array_push($data, $row);
        }
        return $data;
    }
}
?>